package com.yh.controller;

import com.sun.net.httpserver.HttpsServer;
import com.yh.dao.AdminDao;
import com.yh.model.Admin;
import com.yh.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by YH on 2017/9/4.
 */

/**
 * 1 明文密码登录与鉴定 (使用Mybatis 进行数据持久化)
 * 2 明文登录 分开鉴定 username password
 * 3 密码加密
 * 4 注册
 * 5 用户信息更新
 * 6 session
 * 7 cookie
 * 8 jsp
 * 9 ajax
 * 10 sessionID
 */

@Controller
public class WebController {


    //-----------------------------
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private AdminService adminService;

    @RequestMapping("/index")
    public ModelAndView indexPage() {
        return new ModelAndView("index");
    }

    @RequestMapping("/login")
    public ModelAndView loginPage() {
        return new ModelAndView("login");
    }

    @RequestMapping("/register")
    public ModelAndView registerPage() {
        ModelAndView registerView = new ModelAndView();
        registerView.setViewName("register");
        return registerView;
    }

    @RequestMapping(value = "/web/admin/login", method = RequestMethod.POST)
    public ModelAndView findByNamePassword(@RequestParam(value = "userName", required = true) String userName,
                                           @RequestParam(value = "password") String password) {
        if (adminService.findByName(userName).equals("") == false) {
            //拿到 mybatis 记录中密码 进行对比
            Admin adminUser = adminService.findByName(userName);
            String adminUserpassword = adminUser.getPassword();
            if (adminUserpassword.equals(password) == true) {
                return new ModelAndView("index");
            } else {
                System.out.print("用户名或密码不正确");
                return new ModelAndView("login");
            }
        } else {
            System.out.print("用户名不存在");
            return new ModelAndView("login");
        }

//        if ( adminService.findByNameAndPassword(userName,password).equals("") == false){
//            System.out.print(adminService.findByNameAndPassword(userName, password) + "查询到的结果集");
//            return new ModelAndView("index");
//        }else {
//            return new ModelAndView("login");
//        }
    }

    @RequestMapping(value = "/web/admin/register", method = RequestMethod.POST)
    public ModelAndView addAdminUser(@RequestParam(value = "userName", required = true) String userName,
                                     @RequestParam(value = "password") String password) {

        //判断用户名是否存在
        if (adminService.findByName(userName) == null) {
            adminService.addAdminUser(userName, password);
            if (adminService.findByName(userName) != null) {
                System.out.print("注册成功 请登录");
                return new ModelAndView("login");
            }else{
                System.out.print("注册失败 请重试");
                return new ModelAndView("register");
            }
        }else{
            System.out.print("用户已存在");
            return new ModelAndView("register");
        }

    }


}